package com.white.algebra.listener;

import com.white.algebra.utils.MqUtils;

import javax.jms.JMSException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.sql.SQLException;
import java.util.Date;
import java.util.Timer;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ListenerWork implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
        try {
            RedisWork.init();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        executorService.scheduleAtFixedRate(new RedisWork(), 0, 60, TimeUnit.SECONDS);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    MqUtils.innitial();
                    MqUtils.receiveQueueMessage();
                } catch (JMSException | SQLException e) {
                    e.printStackTrace();
                }
            }
        }).start();



    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
//        new RedisWork().run();
    }
}
